﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QldtSdh.BLL;
using QldtSdh.DAL.Models;
using QldtSdh.GUI.Common;
using QldtSdh.GUI.Utils;
using QldtSdh.Utils;

namespace QldtSdh.GUI.DuLieu
{
    public partial class FormQuanLyDotTuyenSinh_ChiTiet : BaseDialog
    {
        DotTuyenSinh dotTuyenSinh;
        int countEntityAdded = 0;
        public event Common.TListChangedEventHandler OnChanged;
        public FormQuanLyDotTuyenSinh_ChiTiet(DotTuyenSinh dotTuyenSinh = null)
        {
            InitializeComponent();
            this.dotTuyenSinh = dotTuyenSinh;
            InitInterface();
        }

        void InitInterface()
        {
            if (dotTuyenSinh != null)
            {
                this.Text = string.Format(TITLE_FORM_DETAIL.EDIT, ENTITY_NAME.COSODAOTAO);
                chkTiepTucBoSung.Checked = false;
                chkTiepTucBoSung.Visible = false;
                txtNam.Text = dotTuyenSinh.NamTuyenSinh.ToString();
                txtDot.Text = dotTuyenSinh.DotTuyenSinh1.ToString();
            }
            else
            {
                this.Text = string.Format(TITLE_FORM_DETAIL.ADDNEW, ENTITY_NAME.COSODAOTAO);
                chkTiepTucBoSung.Visible = true;
                chkTiepTucBoSung.Checked = true;
                txtNam.Text = "";
                txtDot.Text = "";
            }
            txtNam.Focus();
        }

        bool ValidateData()
        {
            errorNotify.Clear();
            if (txtNam.Text.IsNullOrEmpty())
            {
                errorNotify.SetError(txtNam, string.Format(ERROR_CONSTANT_STRING.LOI_MARONG, ENTITY_NAME.DOTTUYENSINH));
                txtNam.Focus();
                return false;
            }
            int i = 0;
            if (!int.TryParse(txtNam.Text, out i))
            {
                errorNotify.SetError(txtNam, ERROR_CONSTANT_STRING.LOI_NUMBER_FORMAT);
                txtNam.Focus();
                return false;
            }

            if (txtDot.Text.IsNullOrEmpty())
            {
                errorNotify.SetError(txtDot, string.Format(ERROR_CONSTANT_STRING.LOI_TENRONG, ENTITY_NAME.DOTTUYENSINH));
                txtDot.Focus();
                return false;
            }
            if (!int.TryParse(txtDot.Text, out i))
            {
                errorNotify.SetError(txtDot, ERROR_CONSTANT_STRING.LOI_NUMBER_FORMAT);
                txtDot.Focus();
                return false;
            }
            return true;
        }

        private void btnDongY_Click(object sender, EventArgs e)
        {
            if (ValidateData())
            {
                if (dotTuyenSinh != null)
                {
                    #region Cập nhật thông tin CSĐT
                    dotTuyenSinh.NamTuyenSinh = int.Parse(txtNam.Text);
                    dotTuyenSinh.DotTuyenSinh1 = int.Parse(txtDot.Text);
                    var rs = BizDotTuyenSinh.Update(dotTuyenSinh);
                    if (rs == CODE_RESULT_RETURN.ThanhCong)
                    {
                        this.DialogResult = System.Windows.Forms.DialogResult.OK;
                    }
                    else if (rs == CODE_RESULT_RETURN.MaTrung)
                    {
                        errorNotify.Clear();
                        errorNotify.SetError(txtNam, string.Format(ERROR_CONSTANT_STRING.LOI_MATRUNG, ENTITY_NAME.DOTTUYENSINH));
                        txtNam.Focus();
                        return;
                    }
                    else if (rs == CODE_RESULT_RETURN.TenTrung)
                    {
                        errorNotify.Clear();
                        errorNotify.SetError(txtDot, string.Format(ERROR_CONSTANT_STRING.LOI_TENTRUNG, ENTITY_NAME.DOTTUYENSINH));
                        txtDot.Focus();
                        return;
                    }
                    else
                    {
                        DialogUtils.ShowError(string.Format(ERROR_CONSTANT_STRING.LOI_CAPNHAT, ENTITY_NAME.DOTTUYENSINH));
                        return;
                    }
                    #endregion
                }
                else
                {
                    #region Thêm mới CSĐT
                    dotTuyenSinh = new DotTuyenSinh();
                    dotTuyenSinh.NamTuyenSinh = int.Parse(txtNam.Text);
                    dotTuyenSinh.DotTuyenSinh1 = int.Parse(txtDot.Text);
                    var rs = BizDotTuyenSinh.Add(dotTuyenSinh);
                    dotTuyenSinh = null;
                    if (rs == CODE_RESULT_RETURN.ThanhCong)
                    {
                        //Sau khi thêm thành công kiểm tra có cho nhập thêm hay không
                        countEntityAdded++;
                        OnChanged(this, null);
                        if (chkTiepTucBoSung.Checked)
                        {
                            InitInterface();
                        }
                        else
                        {
                            this.DialogResult = System.Windows.Forms.DialogResult.OK;
                        }
                    }
                    else if (rs == CODE_RESULT_RETURN.MaTrung)
                    {
                        errorNotify.Clear();
                        errorNotify.SetError(txtNam, string.Format(ERROR_CONSTANT_STRING.LOI_MATRUNG, ENTITY_NAME.COSODAOTAO));
                        txtNam.Focus();
                        return;
                    }
                    else if (rs == CODE_RESULT_RETURN.TenTrung)
                    {
                        errorNotify.Clear();
                        errorNotify.SetError(txtDot, string.Format(ERROR_CONSTANT_STRING.LOI_TENTRUNG, ENTITY_NAME.COSODAOTAO));
                        txtDot.Focus();
                        return;
                    }
                    else
                    {
                        DialogUtils.ShowError(string.Format(ERROR_CONSTANT_STRING.LOI_THEM, ENTITY_NAME.COSODAOTAO));
                        return;
                    }
                    #endregion
                }
            }
        }

        private void FormQuanLyDotTuyenSinh_ChiTiet_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (countEntityAdded > 0)
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }
    }
}
